﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _4DJackPot
{
    public partial class MyResults : Dock
    {
        public MyResults()
        {
            InitializeComponent();
        }

        private void MyResults_Load(object sender, EventArgs e)
        {
            cBx_mon.Checked = true;
            cBx_wed.Checked = true;
            cBx_sat.Checked = true;
            cBx_sun.Checked = true;

            dTP_from.Value = DateTime.Now.AddDays(-7);
        }

        public string Left(string sString, int nLength)
        {
            string sReturn;

            //추출할 갯수가 문자열 길이보다 긴지?
            if (nLength > sString.Length)
            {
                //길다!

                //길다면 원본의 길이만큼 리턴해 준다.
                nLength = sString.Length;
            }

            //문자열 추출
            sReturn = sString.Substring(0, nLength);

            return sReturn;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.dTP_from.Value > this.dTP_to.Value)
            {
                MessageBox.Show("Check Date !", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            this.dGV_myResults.Rows.Clear();
            //select query 작성
            string selectQueryString =
                @"SELECT BET_DATETIME, DRAW_DATETIME, COMPANY_NAME, FORECAST, BET_NUMBER, BET_AMOUNT, BET_WINNING_AMOUNT
                FROM ( SELECT * FROM BETS LEFT JOIN BET_WINNINGS ON 
                     BETS.BET_ID = BET_WINNINGS.BET_ID
                    WHERE 
                     USER_ID = '"+ Global.idString +@"' ) 
                BETRESULT, GAMES, COMPANY_GAMES, COMPANIES
                WHERE LEFT(DRAW_DATETIME,10) >= '" + this.dTP_from.Value.ToShortDateString() + @"'
                    AND LEFT(DRAW_DATETIME,10) <= '" + this.dTP_to.Value.ToShortDateString() + @"'
                AND BETRESULT.GAME_ID = GAMES.GAME_ID
                AND GAMES.GAME_ID = COMPANY_GAMES.GAME_ID
                AND COMPANIES.COMPANY_ID = COMPANY_GAMES.COMPANY_ID 
                ORDER BY BET_DATETIME DESC";
            DbConn DbConn = new DbConn();
            DataSet resultReadDataSet = DbConn.ReadQuery(selectQueryString);

            long totalBetAmountLong = 0;
            long totalAwordAmountLong = 0;
            if (resultReadDataSet.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("There are no results.", "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            foreach (DataRow row in resultReadDataSet.Tables[0].Rows)
            {
                if(!this.cBx_mon.Checked)
                {
                    if (DateTime.Parse(row["DRAW_DATETIME"].ToString()).DayOfWeek == DayOfWeek.Monday)
                        continue;
                }
                if (!this.cBx_sat.Checked)
                {
                    if (DateTime.Parse(row["DRAW_DATETIME"].ToString()).DayOfWeek == DayOfWeek.Saturday)
                        continue;
                }
                if (!this.cBx_wed.Checked)
                {
                    if (DateTime.Parse(row["DRAW_DATETIME"].ToString()).DayOfWeek == DayOfWeek.Wednesday)
                        continue;
                }
                if (!this.cBx_sun.Checked)
                {
                    if (DateTime.Parse(row["DRAW_DATETIME"].ToString()).DayOfWeek == DayOfWeek.Sunday)
                        continue;
                }

                

                int rowIndex = this.dGV_myResults.RowCount;
                this.dGV_myResults.Rows.Insert(rowIndex);
                this.dGV_myResults["dGv_myResults_purchaseDate", rowIndex].Value = row["BET_DATETIME"].ToString();
                this.dGV_myResults["dGv_myResults_drawDate", rowIndex].Value = row["DRAW_DATETIME"].ToString();
                this.dGV_myResults["dGv_myResults_company", rowIndex].Value = row["COMPANY_NAME"].ToString();
                this.dGV_myResults["dGv_myResults_Amount", rowIndex].Value = int.Parse(row["BET_AMOUNT"].ToString()).ToString("###,##0");
                totalBetAmountLong += long.Parse(row["BET_AMOUNT"].ToString());
                string betWinningAmountString = "";
                if (row["BET_WINNING_AMOUNT"].ToString().Equals(""))
                {
                    betWinningAmountString = "";
                }
                else
                {
                    betWinningAmountString = int.Parse(row["BET_WINNING_AMOUNT"].ToString()).ToString("###,##0");
                    totalAwordAmountLong += long.Parse(row["BET_AMOUNT"].ToString());
                }
                this.dGV_myResults["dGv_myResults_awardedAmount", rowIndex].Value = betWinningAmountString;
                string betNumberString = row["BET_NUMBER"].ToString() + "0000";
                betNumberString = Left(betNumberString, 4);
                this.dGV_myResults["dGv_myResults_Number", rowIndex].Value = betNumberString;
                this.dGV_myResults["dGv_myResults_bigsmall", rowIndex].Value = row["FORECAST"].ToString();
                // 추첨일자가 현재 이후일 경우 아직 추첨되지 않음
                if (DateTime.Now < DateTime.Parse(row["DRAW_DATETIME"].ToString()))
                {
                    this.dGV_myResults["dGv_myResults_result", rowIndex].Value = "-";
                }
                else if(row["BET_WINNING_AMOUNT"].ToString().Equals(""))
                {
                    this.dGV_myResults["dGv_myResults_result", rowIndex].Value = "Lose";
                }
                else
                {
                    this.dGV_myResults["dGv_myResults_result", rowIndex].Value = "Win";
                }
            }

            lB_totalBet.Text = "Total Bet : " + totalBetAmountLong.ToString("###,##0") + " KRW ";
            lB_totalAword.Text = "Awarded Bet : " + totalAwordAmountLong.ToString("###,##0") + " KRW ";
        }
    }
}
